// /scripts/c.mjs

import fs from 'fs'
import { projectId, tokenPath ,filter} from './a.mjs'
import { build, get } from './b.mjs'

const API_FOX_COOKIE = fs.existsSync(tokenPath) ? fs.readFileSync(tokenPath, { encoding: 'utf-8' }) : ''
if (API_FOX_COOKIE) {
	// 获取api列表
	let apiList = await get({
		name: 'details',
		Authorization: 'Bearer ' + API_FOX_COOKIE.trim(),
		projectId,
		saveCache: true, // 是否缓存接口原始数据
		filter: filter,
		sort: (a, b) => a.path.localeCompare(b.path)
	})
	if (apiList) {
		// 获取api对应的schema
		let schemas = await get({
			name: 'schemas',
			Authorization: 'Bearer ' + API_FOX_COOKIE.trim(),
			projectId,
			saveCache: true // 是否缓存接口原始数据
		})
		if (schemas) {
			build(apiList, schemas)
			console.log('\nAPI文档已生成:\n/src/api/index.ts')
			console.log('/src/typed/request.d.ts')
			console.log('/src/typed/response.d.ts')
			console.log('/src/typed/response.value.ts\n\n')
		}
	}
} else {
	console.log(`
	没有找到/scripts/token文件
	请把 apiFox 的登录 token字符串，保存到scripts/token文件中
	注意：去掉前面的\`Bearer \`
	`)
}
